﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.IO;
using Common;

public partial class website_admin_sales_ordermaglist : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PageBegin();

        switch (RequestBase.GetFormString("otype"))
        {
            case "loadingdata":
                LoadingData();
                break;
            case "delete":
                Delete();
                break;
        }
    }

    #region 信息绑定
    void LoadingData()
    {
        string ctrl = "~/website_admin/sales/_controls/ctlOrderList.ascx";

        StringBuilder SQL = new StringBuilder();

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("username")))
        {
            SQL.Append(" AND (UserName LIKE '%" + RequestBase.GetFormString("username") + "%' OR FullName LIKE '%" + RequestBase.GetFormString("username") + "%' OR ShipToName LIKE '%" + RequestBase.GetFormString("username") + "%')");
        }

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("ordernumber")))
        {
            SQL.Append(" AND OrderNumber = '" + RequestBase.GetFormString("ordernumber") + "'");
        }

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("orderstatus")))
        {
            SQL.Append(" AND StatusCode = " + RequestBase.GetFormString("orderstatus"));
        }

        if (DateUtil.IsDateTime(RequestBase.GetFormString("date_from")) && DateUtil.IsDateTime(RequestBase.GetFormString("date_to")))
        {
            SQL.Append(" AND (DateDiff(DAY, OrderDate,'" + RequestBase.GetFormString("date_from") + "') <= 0 AND DateDiff(DAY, OrderDate,'" + RequestBase.GetFormString("date_to") + "') >= 0 ) ");
        }
        else
        {
            if (DateUtil.IsDateTime(RequestBase.GetFormString("date_from")))
                SQL.Append(" AND DateDiff(DAY, OrderDate, '" + RequestBase.GetFormString("date_from") + "')=0 ");

            if (DateUtil.IsDateTime(RequestBase.GetFormString("date_to")))
                SQL.Append(" AND DateDiff(DAY, OrderDate, '" + RequestBase.GetFormString("date_to") + "')=0 ");
        }

        Control uc_rep = LoadControl(ctrl);

        Repeater rptOrderMagList = (Repeater)uc_rep.FindControl("rptOrderMagList");

        DBUtility.Pagination pagination = new DBUtility.Pagination();
        try
        {
            if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
            else
                pagination.Sequence = 1;

            pagination.ItemUnit = "条记录";
            pagination.SortSentence = " ORDER BY OrderDate DESC";
            pagination.Primarykey = "OrderId";
            pagination.SearchField = "*";
            pagination.TableName = "vw_OrderBase";
            pagination.PageSize = 30;
            pagination.UrlStr = "";
            pagination.ImagesPath = "../_imgs/grid/";
            pagination.SearchSentence = SQL.ToString();

            rptOrderMagList.DataSource = pagination.GetDataTable();
            rptOrderMagList.DataBind();

            pagination.PageBarFerrari();
            uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            rptOrderMagList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
    #endregion

    #region 删除
    /// <summary>
    /// 删除会员帐号
    /// </summary>
    void Delete()
    {
        if (BLL.MembershipBase.DeleteUser(RequestBase.GetFormString("objectId")))
        {
            BLL.AjaxHelper.ResponseWrite("succeeded");
        }
        else { BLL.AjaxHelper.ResponseWrite("faild"); }
    }
    #endregion
}
